package com.kyzh.core.activities

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Task
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.kyzh.core.utils.loadImage
import kotlinx.android.synthetic.main.activity_new_task.*

class NewTaskActivity : BaseActivity() {

    private val bean = arrayListOf<Task>()
    private val adapter = TaskCenterAdapter(R.layout.simple_list_item_4, bean)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new_task)
        rvList.adapter = adapter
        close.setOnClickListener { finish() }
        WealRequest.newTask {
            adapter.addData(this)
        }
    }


    private inner class TaskCenterAdapter(layout:Int,beans:ArrayList<Task>): BaseQuickAdapter<Task, BaseViewHolder>(layout,beans) {


        override fun convert(holder: BaseViewHolder, item: Task) {
            holder.setText(R.id.tvName,item.name)
                    .setText(R.id.tvSummary,item.point)
            holder.getView<ImageView>(R.id.ivIcon).loadImage(item.icon)
            val tvStatus = holder.getView<TextView>(R.id.tvStatus)
            tvStatus.visibility = View.VISIBLE
            tvStatus.setOnClickListener {
                if (item.status =="1"){
                    WealRequest.getTaskPoint(item.type.toString()){
                        item.status = "2"
                        notifyDataSetChanged()
                    }
                }
            }
            tvStatus.text = getString(
                    when(item.status){
                        "0"->R.string.undo
                        "1"->R.string.get
                        else->R.string.geted
                    }
            )
        }
    }
}